<?php

declare(strict_types=1);

namespace Api\Service\v1\Work;

use App\CustHub\Interfaces\OpenWorkApplicationInterface;
use App\CustHub\Mapper\ChRobotMapper;
use App\CustHub\Mapper\ChSeatMapper;
use App\CustHub\Model\ChRobot;
use App\CustHub\Model\ChSeat;
use App\CustHub\Service\LicenseService;
use App\CustHub\Service\OpenWorkService;
use Carbon\Carbon;
use Hyperf\Di\Annotation\Inject;
use Mine\Exception\NormalStatusException;

class SeatApiService
{
    #[Inject]
    protected ChSeatMapper $seatMapper;

    #[Inject]
    protected ChRobotMapper $robotMapper;

    #[Inject]
    protected OpenWorkApplicationInterface $openWorkApplication;

    #[Inject]
    protected LicenseService $licenseService;

    public function list(array $params)
    {
        $pageSize = $params['page_size'] ?? 10;
        $data = ChSeat::query()
            ->select(['id', 'auth_corp_id', 'code', 'use_status', 'expire_time', 'bind_time', 'change_bind_time', 'created_at', 'updated_at'])
            ->where('main_uid', $params['uid'])
            ->where('seat_type', 1)
            ->orderBy('created_at', 'desc')
            ->paginate((int)$pageSize);
        return $this->seatMapper->setPaginate($data);
    }

    public function bind(array $params): bool
    {
        $seat = $this->seatMapper->findById($params['seat_id']);
        if (!$seat) {
            throw new NormalStatusException('席位不存在');
        }

        if ($seat->use_status == 1) {
            throw new NormalStatusException('该席位已绑定');
        }

        if($this->licenseService->checkLicense($seat->auth_corp_id,$params['userid'])){
            throw new NormalStatusException('您已经绑过席位了，解绑后才能绑定新的席位');
        }

        $this->openWorkApplication->activeAccount($seat->code, $seat->auth_corp_id, $params['userid']);

        $this->seatMapper->update($seat->id,[
            'use_status' => 1,
            'userid' => $params['userid'],
            'bind_time' => Carbon::now()
        ]);

        $robot = ChRobot::query()
            ->where('main_uid', $seat->main_uid)
            ->where('userid', $params['userid'])
            ->first();

        if ($robot) {
            $this->robotMapper->update($robot->id,[
                'ht_seat_id' => $seat->id,
                'ht_bind_status' => 1
            ]);
        }
 
        return true;
    }

    public function changeBind(array $params): bool
    {
        $seat = $this->seatMapper->findById($params['seat_id']);
        if (!$seat) {
            throw new NormalStatusException('席位不存在');
        }

        $this->openWorkApplication->transferLicense($seat->auth_corp_id, $params['handover_userid'],$params['takeover_userid']);

        $this->seatMapper->update($seat->id,[
            'userid' => $params['takeover_userid'],
            'change_bind_time' => Carbon::now()
        ]);

        $handoverRobot = ChRobot::query()
            ->where('main_uid', $seat->main_uid)
            ->where('userid', $params['handover_userid'])
            ->first();
        if ($handoverRobot) {
            $this->robotMapper->update($handoverRobot->id,[
                'ht_seat_id' => null,
                'ht_bind_status' => 0
            ]);
        }

        $takeoverRobot = ChRobot::query()
            ->where('main_uid', $seat->main_uid)
            ->where('userid', $params['takeover_userid'])
            ->first();

        if ($takeoverRobot) {
            $this->robotMapper->update($takeoverRobot->id,[
                'ht_seat_id' => $seat->id,
                'ht_bind_status' => 1
            ]);
        }

        return true;
    }

    public function checkLicense(array $params): array
    {
        if((new LicenseService())->checkLicense($params['corp_id'],$params['userid'])){
            $isBind = 1;
        }else{
            $isBind = 0;
        }
        return [
            'is_bind' => $isBind
        ];
    }
}